using System;
using System.Collections.Generic;
using System.Text;

namespace Index.TemporaryFile.Sorting.Merge
{
    public class OutputQueue 
    {
        private int size;
        private TermEntryFile output;
        private Queue<TermEntry> queue;

        public OutputQueue(int size, TermEntryFile output)
        {
            this.size = size;
            this.output = output;
            this.queue = new Queue<TermEntry>(size);
        }

        public int Count
        {
            get
            {
                return queue.Count;
            }
        }

        public void Add(TermEntry item)
        {
            if (queue.Count < size)
            {
                queue.Enqueue(item);
            }
            else
            {
                Flush();
                queue.Enqueue(item);
                
            }
        }

        public void Flush()
        {
            TermEntry[] entries = queue.ToArray();

            if (entries != null)
            {
                output.Append(entries);
            }

            queue.Clear();
        }

    }
}
